class ProjectController < ApplicationController
  before_filter :login_required

  def index
    @projects = session['user'].projects(true).sort_by {|p| p.name.upcase}
    #@projects = Project.find(:all, :conditions=>"", :joins=>"", :order => :created_on)
  end

  def update
    begin
      @project = Project.find(params[:id])
      @project.update_attributes!(params[:project])
      flash[:notice] = 'Project was successfully updated.'
      rescue Exception => e
      flash[:notice] = e.message
    end
    redirect_to :action=>:edit, :id=>@project
  end

  # create a new project
  def add
    begin
      name = params[:new][:name]
      if(name=="")
        flash[:notice] = "The project name can not be empty"
        redirect_to(:action => "index")
        return
      end
      t = Project.find(:first, :include => [:users], :conditions => "projects.name='#{name}' and users.id='#{session['user'].id}'")
      if(t)
        flash[:notice] = "You already have a project called #{t.name}"
        redirect_to(:action => "index")
        return
      end
      t = Project.new(params[:new])
      t.save!
      t.users << session['user']
      flash[:notice] = "#{t.name} has been created and you have been added as member"
      redirect_to(:action => "edit", :id => t)
    rescue Exception => e
      flash[:notice] = e
      redirect_to(:action => "index")
    end

  end

  def edit
    @project = Project.find(params[:id])
    render(:text=>'No permission to edit this project') and return if (not @project.users.member?(session['user']))
    @users = User.find(:all,:order=>"name").map {|u| [u.name, u.id]}
  end

  def view
    @project   = Project.find(params[:id])
    render(:text=>'No permission to view this project') and return if (not @project.users.member?(session['user']))
    session[:selected_project] = @project.id
    @events    = Calendar.find(:all, :conditions => "project_id=#{@project.id}").sort_by {|c| if(c.date==nil) then [Date.today()] else [c.date] end}
    cond       = "project_id='#{@project.id}'"
    @tasks     = Task.find(:all, :conditions=>"done='0' and "+cond, :order=>"`order`")
    @tasksdone = Task.find(:all, :conditions=>"done='1' and "+cond, :order=>"done_on desc", :limit=>5)
    @tags      = @tasks.inject([]) {|tags,t| tags += t.tags}.uniq.sort_by{|t| t.name}
  end

  # Ajaxized
  def filter_by_tag()
    @project = Project.find(params[:id])
    tags  =  params[:t]
    cond_str = "done='0' and project_id=#{@project.id}"
    cond  = {:conditions=>cond_str, :order=>'`order`'}
    if tags == ''
      @tasks = Task.find(:all, :conditions=>cond_str, :order=>"`order`")
    else
      @tasks = Task.find_tagged_with(tags,  cond)
    end
    render(:partial=>'task', :collection=>@tasks)
  end

  def del_user
    p = Project.find(params[:p_id])
    render(:text=>"<strong>last user can not be removed from project</strong>") and return if p.users.size <= 1
    u = User.find(params[:id])
    p.users.delete(u)
    render(:text=>"<i>user removed from this project</i>")
  end

  # Ajaxized
  #def delete
  #  if Project.destroy(params[:id]) == 0
  #    render (:text=>"This project did not exist")
  #  else
  #    render (:text=>"document.getElementById('notice').innerHTML= 'project has been deleted';var el=document.getElementById('tr#{params[:id]}');new Effect.Fade(el,{delay:1});")
  #  end
  #end

end
